<template>
  <div class="xtx-confirm" :class='{fade: fade}'>
    <div class="wrapper" :class='{fade: fade}'>
      <div class="header">
        <h3>{{title}}</h3>
        <a @click='cancelCallback' href="JavaScript:;"></a>
      </div>
      <div class="body">
        <!-- 字体图标 -->
        <!-- <i class="iconfont icon-warning"></i> -->
        <span>{{text}}</span>
      </div>
      <div class="footer">
        <button class="xtx-button mini default" @click='cancelCallback'>取消</button>
        <button class="xtx-button mini primary" @click="submitCallback">确认</button>
      </div>
    </div>
  </div>
</template>
<script>
import { onMounted, ref } from 'vue'
export default {
  name: 'XtxConfirm',
  // components: { XtxButton },
  props: {
    title: {
      type: String,
      default: '提示消息'
    },
    text: {
      type: String,
      default: '确认这样操作吗？'
    },
    submitCallback: {
      type: Function
    },
    cancelCallback: {
      type: Function
    }
  },
  setup () {
    // 控制动画效果
    const fade = ref(false)
    onMounted(() => {
      // 当元素渲染完毕立即过渡的动画不会触发
      setTimeout(() => {
        fade.value = true
      }, 0)
    })
    return { fade }
  }
}
</script>
<style scoped lang="less">
.xtx-button {
  appearance: none;
  border: none;
  outline: none;
  background: #fff;
  text-align: center;
  border: 1px solid transparent;
  border-radius: 4px;
  cursor: pointer;
}
.mini {
  width: 60px;
  height: 32px;
  font-size: 14px;
}
.primary {
  border-color: #27ba9b;
  background: #27ba9b;
  color: #fff;
}
.default {
  border-color: #e4e4e4;
  color: #666;
}
.xtx-confirm {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: 8888;
  background: rgba(0, 0, 0, 0);
  &.fade {
    transition: all 0.4s;
    background: rgba(0, 0, 0, 0.5);
  }
  .wrapper {
    width: 400px;
    background: #fff;
    border-radius: 4px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -60%);
    opacity: 0;
    &.fade {
      transition: all 0.4s;
      transform: translate(-50%, -50%);
      opacity: 1;
    }
    .header,
    .footer {
      height: 50px;
      line-height: 50px;
      padding: 0 20px;
    }
    .body {
      padding: 20px 40px;
      font-size: 16px;
      .icon-warning {
        color: @priceColor;
        margin-right: 3px;
        font-size: 16px;
      }
    }
    .footer {
      text-align: right;
      .xtx-button {
        margin-left: 20px;
      }
    }
    .header {
      position: relative;
      h3 {
        font-weight: normal;
        font-size: 18px;
      }
      a {
        position: absolute;
        right: 15px;
        top: 15px;
        font-size: 20px;
        width: 20px;
        height: 20px;
        line-height: 20px;
        text-align: center;
        color: #999;
        &:hover {
          color: #666;
        }
      }
    }
  }
}
</style>
